//
//  XXLabel.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/3/28.
//

import UIKit

class XXLabel: UILabel {

    var tapBlock:XXVoidBlock?{
        didSet{
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(lblTapAction))
            self.isUserInteractionEnabled = true
            self.addGestureRecognizer(tap)
        }
    }
    @objc func lblTapAction(){
        if let block = tapBlock{
            block()
        }
    }
}

extension XXLabel{
    
    convenience init(_ text:String = "") {
        self.init(frame: .zero, text: text, font: .system(15), color: .black, textAlignment: .left)
    }
    
    convenience init(_ text:String = "", _ font:UIFont = .system(15), _ color:UIColor = .black) {
        self.init(frame: .zero, text: text, font: .system(15), color: .black, textAlignment: .left)
    }
    
    convenience init(frame:CGRect = .zero, text:String = "", font:UIFont = .system(15), color:UIColor = .black, textAlignment: NSTextAlignment = .left) {
        self.init()
        self.frame = frame
        self.text = text
        self.textColor = color
        self.textAlignment = textAlignment
        self.font = font
        self.numberOfLines = 0
    }
}

extension XXLabel{

    enum XXLabelStyle{
        case system15Black(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case system13Black(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case system13Hex333(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case system13Hex8080(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case bold15Black(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case bold13Black(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case bold16Hex333(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case medium15Black(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case medium13Black(_ text:String = "",_ alignment:NSTextAlignment = .left)
        case medium16Hex333(_ text:String = "",_ alignment:NSTextAlignment = .left)
    }
    
    
    
    convenience init(_ style:XXLabelStyle){
        self.init()
        switch style {
        case let .system15Black(text,alignment):
            setStyle(text: text, font: .system(15), color: .black, textAlignment: alignment)
        case let .system13Black(text,alignment):
            setStyle(text: text, font: .system(13), color: .black, textAlignment: alignment)
        case let .system13Hex333(text,alignment):
            setStyle(text: text, font: .system(13), color: .x333, textAlignment: alignment)
        case let .system13Hex8080(text,alignment):
            setStyle(text: text, font: .system(13), color: .hex("#808080"), textAlignment: alignment)
        case let .bold15Black(text,alignment):
            setStyle(text: text, font: .bold(15), color: .black, textAlignment: alignment)
        case let .bold13Black(text,alignment):
            setStyle(text: text, font: .bold(13), color: .black, textAlignment: alignment)
        case let .bold16Hex333(text,alignment):
            setStyle(text: text, font: .bold(13), color: .x333, textAlignment: alignment)
        case let .medium15Black(text,alignment):
            setStyle(text: text, font: .medium(15), color: .black, textAlignment: alignment)
        case let .medium13Black(text,alignment):
            setStyle(text: text, font: .medium(13), color: .black, textAlignment: alignment)
        case let .medium16Hex333(text,alignment):
            setStyle(text: text, font: .medium(16), color: .x333, textAlignment: alignment)
        default:
            break
        }
    }
    
    func setStyle(text:String = "", font:UIFont = .system(15), color:UIColor = .black, textAlignment: NSTextAlignment = .left){
        self.text = text
        self.textColor = color
        self.textAlignment = textAlignment
        self.font = font
        self.numberOfLines = 0
    }
}
